<?php

namespace App\Admin\Actions\Grid\Row\SwtOrder;

use App\Http\Logic\OrderLogic;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class CancelOrder extends RowAction
{
    /**
     * @return string
     */
    protected $title = '取消';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $out_order_no = $request->get('order_sn');
        $door_num = $request->get('door_num');
        $platform = $request->get('platform');

        OrderLogic::cancelOrder($out_order_no, $door_num, $platform);

        return $this->response()
            ->success('取消成功')
            ->refresh();
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return ['确认操作吗?', '更拉取最新状态以更新设备信息'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [
            'order_sn'   => $this->row->order_sn,
            'door_num'   => $this->row->door_num,
            'platform'   => $this->row->platform
        ];
    }

    protected function html()
    {
        return <<<HTML
            <a {$this->formatHtmlAttributes()}><i class="feather icon-x text-danger"></i> <span class="text-danger">{$this->title}</span> &emsp;</a>
        HTML;
    }
}
